package com.jx.service.content;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jx.common.exception.ServiceException;
import com.jx.dao.content.ArticleDao;
import com.jx.pojo.entity.content.Article;
import com.jx.pojo.entity.content.ArticleContent;
import com.jx.pojo.enums.system.StatusEnum;
import com.jx.pojo.model.content.ArticleDetail;
import com.jx.pojo.po.content.ArticlePO;
import com.jx.pojo.po.content.ItemPO;
import com.jx.pojo.vo.content.ArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章
 */
@Service
@Slf4j
public class ArticleService extends ServiceImpl<ArticleDao, Article> {

    @Resource
    private ArticleContentService articleContentService;

    @Resource
    private ArticleDao articleDao;

    @Resource
    private CategoryService categoryService;

    public PageInfo<Article> listPage(ArticleVO query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(query.getTitle()), "title", query.getTitle());
        wrapper.like(StringUtils.isNotBlank(query.getAuthor()), "author", query.getAuthor());
        wrapper.eq(StringUtils.isNotBlank(query.getCategoryId()), "category_id", query.getCategoryId());
        wrapper.eq(query.getStatus() != null, "status", query.getStatus());
        wrapper.ge(StringUtils.isNotBlank(query.getStartTime()), "publishDate", query.getStartTime());
        wrapper.le(StringUtils.isNotBlank(query.getEndTime()), "publishDate", query.getEndTime());
        List<Article> list = this.list(wrapper);

        // 获取类别
        Map<Integer, String> categoryMap = categoryService.getCategoryMap();
        for (Article article : list) {
            article.setCategoryName(categoryMap.get(article.getCategoryId()));
        }
        return new PageInfo<>(list);
    }

    /**
     * 保存文章
     */
    @Transactional
    public void saveArticle(ArticleDetail articleDetail) {
        // 保存文章信息
        Article article = new Article();
        BeanUtils.copyProperties(articleDetail, article);
        LocalDateTime dateTime = LocalDateTime.now();
        if (articleDetail.getId() == null) {
            article.setCreateTime(dateTime);
        }
        article.setUpdateTime(dateTime);
        if (article.getPublishTime() == null) {
            article.setPublishTime(dateTime);
        }
        articleDao.insertOrUpdate(article);

        // 保存文章内容
        ArticleContent articleContent = new ArticleContent();
        articleContent.setArticleId(article.getId());
        articleContent.setContent(articleDetail.getContent());
        articleContentService.saveArticleContent(articleContent);
    }

    public ArticleDetail getArticleById(Integer id) {
        Article article = articleDao.selectById(id);

        if (article == null) {
            throw new ServiceException("文章不存在");
        }

        ArticleContent articleContent = articleContentService.getArticleContentByArticleId(id);

        ArticleDetail articleDetail = new ArticleDetail();
        BeanUtils.copyProperties(article, articleDetail);
        articleDetail.setContent(articleContent.getContent());
        return articleDetail;
    }

    @Transactional
    public void deleteById(Integer id) {
        articleDao.deleteById(id);
        articleContentService.deleteByArticleId(id);
    }

    public List<Article> listAll() {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusEnum.NORMAL.getCode());
        return this.list(wrapper);
    }

    public List<ItemPO> getArticleCategoryTopN(Integer n) {
        return articleDao.getArticleCategoryTopN(n);
    }

    /**
     * 根据关键词获取文章列表
     */
    public List<Article> getArticleListByKeyword(String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return new ArrayList<>();
        }
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.like("title", keyword);
        wrapper.eq("status", StatusEnum.NORMAL.getCode());
        return this.list(wrapper);
    }
}
